package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.entity.CompleteUserInfo;
import com.tiza.xiaoyoubao.entity.OrderUserInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;
import com.tiza.xiaoyoubao.utils.DateUtils;

import org.simple.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by mapple on 17/4/3.
 */

public class JiHuoUserFragment extends BaseFragment {

    private String info;
    @BindView(R.id.rb_order_user)
    RadioButton rb_order_user;
    @BindView(R.id.rb_complete_user)
    RadioButton rb_complete_user;
    @BindView(R.id.view_orderUser)
    View view_orderUser;
    @BindView(R.id.view_CompleteUser)
    View view_CompleteUser;
    @BindView(R.id.lv_listView)
    ListView lv_listView;
    private List<OrderUserInfo> orderUserList;
    private List<CompleteUserInfo> completeUserList;
    private OrderUserAdapter mOrderUserAdapter;
    private CompleteUserAdapter mCompleteUserAdapter;
    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);
    private String orderNum,completeNum;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_jihuo_user;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        info = args.getString("info");
        orderNum=args.getString("orderNum");
        completeNum=args.getString("completeNum");
        if (null == info) {
            toast("详情加载失败");
            finish();
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        // 通知跳转成功
        EventBus.getDefault().post(true, Constants.TAG_STATE_SUCCESS);
        rb_order_user.setText("已接单"+orderNum+"人");
        rb_complete_user.setText("已完成"+completeNum+"人");
        if (info.equals("order_user")) {
            view_CompleteUser.setVisibility(View.GONE);
            view_orderUser.setVisibility(View.VISIBLE);
            rb_order_user.setChecked(true);
            rb_order_user.setTextColor(getResources().getColor(R.color.white));
            getBuyerInfo();
        } else if (info.equals("complete_user")) {
            view_CompleteUser.setVisibility(View.VISIBLE);
            view_orderUser.setVisibility(View.GONE);
            rb_complete_user.setChecked(true);
            rb_complete_user.setTextColor(getResources().getColor(R.color.white));
            getBuyerInfoFinish();
        }
    }

    @OnClick({
            R.id.rb_order_user, R.id.rb_complete_user,
    })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_order_user://用户已接单
                view_CompleteUser.setVisibility(View.GONE);
                view_orderUser.setVisibility(View.VISIBLE);
                rb_order_user.setTextColor(getResources().getColor(R.color.white));
                rb_complete_user.setTextColor(getResources().getColor(R.color.steel_blue));
                getBuyerInfo();
                break;
            case R.id.rb_complete_user://用户已完成
                view_CompleteUser.setVisibility(View.VISIBLE);
                view_orderUser.setVisibility(View.GONE);
                rb_complete_user.setTextColor(getResources().getColor(R.color.white));
                rb_order_user.setTextColor(getResources().getColor(R.color.steel_blue));
                getBuyerInfoFinish();
                break;
            default:
                break;
        }
    }

    class OrderUserAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return orderUserList.size();
        }

        @Override
        public Object getItem(int position) {
            return orderUserList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view == null) {
                view = View.inflate(getContext(), R.layout.item_order_user, null);
                holder = new ViewHolder();
                holder.userId = (TextView) view.findViewById(R.id.tv_userId);
                holder.firstOrderTime = (TextView) view.findViewById(R.id.tv_FirstOeder_Time);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.userId.setText(orderUserList.get(position).getBuyerID());
            holder.firstOrderTime.setText(DateUtils.getDateToString(orderUserList.get(position).getAddTime()));
            return view;
        }
    }

    class CompleteUserAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return completeUserList.size();
        }

        @Override
        public Object getItem(int position) {
            return completeUserList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view == null) {
                view = View.inflate(getContext(), R.layout.item_complete_user, null);
                holder = new ViewHolder();
                holder.userId = (TextView) view.findViewById(R.id.tv_userId);
                holder.registeredTime = (TextView) view.findViewById(R.id.tv_registeredTime);
                holder.comminssion = (TextView) view.findViewById(R.id.tv_commission);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.userId.setText(completeUserList.get(position).getBuyerID());
            holder.registeredTime.setText(DateUtils.getDateToString(completeUserList.get(position).getRegisterTime()));
            holder.comminssion.setText(completeUserList.get(position).getCmi());
            return view;
        }
    }

    static class ViewHolder {
        public TextView userId;
        public TextView registeredTime;
        public TextView firstOrderTime;
        public TextView comminssion;
    }

    //已接单
    private void getBuyerInfo() {
        ApiHttpClient.getBuyerInfo(JiHuoUserFragment.this, userInfo.getWebzzCmsBuyerInfo().getUserid(), "2",
                new JsonCallback<OrderUserInfo>(OrderUserInfo.class) {

                    @Override
                    public void onBefore(Request request) {
                        super.onBefore(request);
                        showWaitingDialog();
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        toast("登录失败");
                    }

                    @Override
                    public void onResponse(ResponseInfo<OrderUserInfo> response) {
                        int code = response.getCode();
                        String message = response.getMessage();
                        if (code == 1) {
                            showWarnDialog(message);
                            return;
                        }
                        orderUserList = response.getData();
                        if (null == orderUserList || orderUserList.size() == 0) {
                            toast("获取信息失败");
                            return;
                        }
                        mOrderUserAdapter = new OrderUserAdapter();
                        lv_listView.setAdapter(mOrderUserAdapter);
                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                        hideWaitingDialog();
                    }
                }
        );
    }

    //已完成
    private void getBuyerInfoFinish() {
        ApiHttpClient.getBuyerInfoFinish(JiHuoUserFragment.this, userInfo.getWebzzCmsBuyerInfo().getUserid(), "2",
                new JsonCallback<CompleteUserInfo>(CompleteUserInfo.class) {

                    @Override
                    public void onBefore(Request request) {
                        super.onBefore(request);
                        showWaitingDialog();
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        toast("获取信息失败，请重试");
                    }

                    @Override
                    public void onResponse(ResponseInfo<CompleteUserInfo> response) {
                        int code = response.getCode();
                        String message = response.getMessage();
                        if (code == 1) {
                            showWarnDialog(message);
                            return;
                        }
                        completeUserList = response.getData();
                        if (null == completeUserList || completeUserList.size() == 0) {
                            toast("获取信息失败");
                            return;
                        }
                        mCompleteUserAdapter = new CompleteUserAdapter();
                        lv_listView.setAdapter(mCompleteUserAdapter);
                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                        hideWaitingDialog();
                    }
                }
        );
    }
}
